// pages/myfoodlist/myfoodlist.js
var util=require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr3: [
      
      { id: 1, img: "../../img/food2.jpg", name: "五花肉石锅拌饭", num: "0", price: "51", oprice: "51", idt: "配米饭一份哦", message2: "月售330｜好评率100%", message3: '' },
      { id: 2, img: "../../img/food2.jpg", name: "五花肉石锅拌饭", num: "0", price: "51", oprice: "51", idt: "配米饭一份哦", message2: "月售330｜好评率100%", message3: [{ id: 0, value: "香辣味2" }, { id: 1, value: "盐焗味2" }, { id: 2, value: "蒜香味2" }, { id: 3, value: "姜葱味2" },] },
      

    ],
    shop:5
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;

    util.fetchGet(
      util.url.shop_food_list,
      function(ret){
        console.log(ret.data);
        var arr=[]
        for(var i=0;i<ret.data.length;i++){
          var ft=ret.data[i];
          var food={};
          food.id = ft.id;
          food.img = wx.getStorageSync('imghost') +ft.pic;
          food.name=ft.name;
          food.price=ft.price;
          food.oprice=ft.oprice;
          food.idt=ft.idt
          food.state=ft.state
          arr.push(food);
        } 
        that.setData({
          arr3:arr
        })
      }
    )
  },
  goToSetFood:function(e){
    wx.navigateTo({
      url: 'set?id=' + e.currentTarget.dataset.foodid,
    })
  },
  goToSetFoodState:function(e){
    var that=this;
    var foodid = e.currentTarget.dataset.foodid;
    var state = e.currentTarget.dataset.foodstate;
    state=state==0?1:0;
    util.fetchGet( 
      util.url.shop_food_updatefoodstate + '?id=' + foodid + '&state=' + state,
      function(ret){
        for(var i=0;i<that.data.arr3.length;i++){
          if (that.data.arr3[i].id == foodid){
            that.data.arr3[i].state = state;
          }
        }
        that.data.arr3.sort(function(a,b){return a.state-b.state})
        that.setData({
          arr3: that.data.arr3
        })
      }
    )
  }
})